# -*- coding: utf-8 -*-
# @Time    : 18-8-3 下午10:19
# @Author  : hp
# @Email   : shaoeric@foxmail.com

from qqbot import QQBotSlot as qqbotslot, RunBot, qqbotsched
from qqbot import _bot as qbot
from qqbot.qcontactdb.contactdb import Group, Buddy
import requests
from turing import getresponse
from weather import getWeather

@qqbotslot
def onQQMessage(bot, contact, member, content):
    # member只针对qq群， contact是消息来源（群和好友）

    if '@ME' in content:
        msg = content.replace("[@ME]", '')
        response = getresponse(msg=msg, userId=str(member.uin))
        bot.SendTo(contact, '@'+member.name + ' {}'.format(response))

    if contact.name == "闪电侠的右手" and isinstance(contact, Buddy):
        if content == '-hello':
            bot.SendTo(contact, '你好，我是QQ机器人')
        elif content == '在':
            bot.SendTo(contact, '我一直在呢，嘻嘻')
        elif content == '-stop':
            bot.SendTo(contact, 'QQ机器人已关闭')
            bot.Stop()
        elif content == "-update":
            bot.SendTo(contact, "正在更新群列表")
            flag = bot.Update('group')
            if flag:
                lst = bot.List('group')
                bot.SendTo(contact, "群列表更新成功，当前群:\n{}".format(str(lst)))
            else:
                bot.SendTo(contact, "群列表更新失败")

@qqbotsched(hour='20', minute='30')
def task(bot):
    weather = getWeather()
    groups = bot.List('group')
    for group in groups:
        bot.SendTo(group, weather)



if __name__ == '__main__':
    # qbot.Login(['-q', '1557328130'])
    # RunBot()
    qbot.Login()
    qbot.Run()